package com.zmn.plat.admin.controller.product.businessline;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.model.entity.product.businessline.ProductBusinessLine;
import com.zmn.plat.model.entity.product.businessline.ProductBusinessLineQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.common.utils.office.ExcelUtil;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.plat.admin.controller.product.productgroup.ProductGroupBaseController;
import com.zmn.plat.business.interfaces.product.productgroup.ProductGroupBService;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.model.entity.product.productgroup.ProductGroup;
import com.zmn.plat.model.entity.product.productgroup.ProductGroupQuery;
import com.zmn.plat.services.interfaces.product.productgroup.ProductGroupService;

import lombok.extern.slf4j.Slf4j;

/**
 * 产品业务线基础查询Controller
 *
 * @author tanbiao
 * @since 2020/10/27 14:52
 */
@Controller
@RequestMapping("/product/businessline/")
@Slf4j
public class ProductBusinessLineListController extends ProductBusinessLineBaseController {


    /**
     * 列表
     */
    @RequestMapping("list")
    @RequiresPermissions("/product/businessline/list.action")
    public ModelAndView list(@ModelAttribute ProductBusinessLineQuery query) {
        List<ProductBusinessLine> items = productBusinessLineService.listPageByQuery(query);

        ModelAndView mav = new ModelAndView("product/businessline/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);

        return mav;
    }

    @RequestMapping(value = "listVtByBizType")
    @ResponseBody
    public ResponseDTO listVtByBizType(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "bizType") int bizType) {
        List<VtDTO> list = productBusinessLineBService.listVtByBizType(bizType,GlobalConsts.YES);
        return ResponseDTO.success(list);
    }

    /**
     * 信息
     */
    @RequestMapping("addEditView")
    @RequiresPermissions("/product/businessline/addEditView.action")
    public ModelAndView addEditView(@RequestParam(value = "relateId", defaultValue = "0") Integer relateId) {

        ProductBusinessLine productBusinessLine;
        if (relateId == 0) {
            productBusinessLine = new ProductBusinessLine();
            productBusinessLine.setRelateId(0);
        } else {
            productBusinessLine = productBusinessLineService.findById(relateId);
        }

        ModelAndView mav = new ModelAndView("product/businessline/detail");
        mav.addObject("productBusinessLine", productBusinessLine);
        mav.addObject("bizTypeList", ProductDict.listBizType());
        return mav;
    }

    /**
     * 描述: 根据条件导出产品Excel
     *
     * @param query
     *            查询条件
     * @author wangjie
     * @since 11:43 2019/2/19
     **/
    @RequestMapping(value = "outputExcel")
    @RequiresPermissions("/product/businessline/list.action")
    public void outputExcel(HttpServletResponse response, ProductBusinessLineQuery query) {

        Integer pageSize = getExportPageLimit(response);
        query.setPageSize(pageSize);
        ArrayList<List> lists = productBusinessLineBService.outputProductExcel(query);
        try {
            String name = "产品业务线报表.xlsx";
            name = new String(name.getBytes(), "ISO8859-1");
            ExcelUtil.export(response, lists, name, "sheet");
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "导出报表出错" + e.getMessage(), e);
        }
    }

}
